package cn.edu.njpi.service.impl;

import cn.edu.njpi.mapper.TClassMapper;
import cn.edu.njpi.mapper.TCollageMapper;
import cn.edu.njpi.mapper.TSchoolMapper;
import cn.edu.njpi.model.TClass;
import cn.edu.njpi.model.TCollage;
import cn.edu.njpi.model.TSchool;
import cn.edu.njpi.service.TClassService;
import cn.edu.njpi.utils.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.service.impl
 * @date 2022/4/20 8:19 PM
 */
@Service
public class TClassServiceImpl implements TClassService {
    @Autowired
    private TClassMapper tClassMapper;
    @Autowired
    private TCollageMapper tCollageMapper;
    @Autowired
    private TSchoolMapper tSchoolMapper;

    @Override
    public List<TClass> getAllClass() {
        List<TClass> tClassList = tClassMapper.getAllClass();
        getCollageAndSchool(tClassList);
        tClassList.sort(Comparator.comparingInt(TClass::getSchoolId));
        return tClassList;
    }

    @Override
    public List<TClass> selectClassBySchoolId(Integer schoolId) {
        List<TClass> tClassList = tClassMapper.getAllClass();
        List<TClass> tClassList1 = new ArrayList<>();
        for (TClass tClass : tClassList) {
            TCollage tCollage = tCollageMapper.selectByPrimaryKey(tClass.getCollageId());
            TSchool tSchool = tSchoolMapper.selectByPrimaryKey(tCollage.getSchoolId());
            tClass.setSchoolId(tSchool.getSchoolId());
            tClass.setSchoolName(tSchool.getSchoolName());
            tClass.setCollageName(tCollage.getCollageName());
            if (Objects.equals(tClass.getSchoolId(), schoolId)) {
                tClassList1.add(tClass);
            }
        }
        tClassList1.sort(Comparator.comparingInt(TClass::getCollageId));
        return tClassList1;
    }

    @Override
    public List<TClass> selectClassByCollageId(Integer collageId) {
        List<TClass> tClassList = tClassMapper.selectClassByCollageId(collageId);
        getCollageAndSchool(tClassList);
        return tClassList;
    }

    @Override
    public T addClass(TClass tClass) {
        T t = new T();
        TClass tClass1 = tClassMapper.selectClassByClassNameAndCollageId(tClass);
        if (tClass1 == null) {
            tClassMapper.insertSelective(tClass);
            t.setStatus(200);
        } else {
            t.add("error", "该班级已存在");
            t.setStatus(503);
        }
        return t;
    }

    @Override
    public T changeClass(TClass tClass) {
        T t = new T();
        TClass tClass2 = tClassMapper.selectByPrimaryKey(tClass.getClassId());
        if (tClass2 != null) {
            tClass.setCollageId(tClass2.getCollageId());
            TClass tClass1 = tClassMapper.selectClassByClassNameAndCollageId(tClass);
            if (tClass1 == null) {
                tClass2.setClassName(tClass.getClassName());
                tClassMapper.updateByPrimaryKeySelective(tClass2);
                t.setStatus(200);
            } else {
                t.add("error", "该班级已存在");
                t.setStatus(503);
            }
        } else {
            t.add("error", "该班级不存在");
            t.setStatus(503);
        }
        return t;
    }

    private void getCollageAndSchool(List<TClass> tClassList) {
        for (TClass tClass : tClassList) {
            TCollage tCollage = tCollageMapper.selectByPrimaryKey(tClass.getCollageId());
            TSchool tSchool = tSchoolMapper.selectByPrimaryKey(tCollage.getSchoolId());
            tClass.setSchoolId(tSchool.getSchoolId());
            tClass.setSchoolName(tSchool.getSchoolName());
            tClass.setCollageName(tCollage.getCollageName());
        }
    }
}
